home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / dns / ddt2.0 / cmd / mxc < prev    next >
Encoding:
Text File  |  1993-05-24  |  4.7 KB  |  202 lines

  1. #! /usr/local/bin/perl
  2. #
  3. #  mxc - mail exchange (MX) checker
  4. #
  5. #  invoke as:
  6. #    mxc [-l msglevel]
  7. #
  8. #  Copyright (C) 1992, 1993 PUUG - Grupo Portugues de Utilizadores
  9. #                   do Sistema UNIX
  10. #             1992, 1993 FCCN - Fundacao para o Desenvolvimento dos Meios 
  11. #                        Nacionais de Calculo Cientifico     
  12. #
  13. #  Authors: Jorge Frazao de Oliveira <frazao@puug.pt>
  14. #        Artur Romao <artur@dns.pt>
  15. #
  16. #  This file is part of the DDT package, Version 2.0.
  17. #
  18. #  Permission to use, copy, modify, and distribute this software and its 
  19. #  documentation for any purpose and without any fee is hereby granted, 
  20. #  provided that the above copyright notice appear in all copies.  Neither 
  21. #  PUUG nor FCCN make any representations about the suitability of this
  22. #  software for any purpose.  It is provided "as is" without express or 
  23. #  implied warranty.
  24.  
  25.  
  26. # =()<push(@INC, "@<LIBDIR>@");>()=
  27. push(@INC, "/usr/local/lib/ddt/cmd");
  28.  
  29. require "ddt.pl";
  30.  
  31.  
  32. # scan the standard input
  33. while (<STDIN>) {
  34.     next if /^;/;            # ignore commented lines
  35.  
  36.     chop;                # strip record separator    
  37.     @Field = split(/\s+/, $_);    # break the input line
  38.  
  39.     if (/^\$ORIGIN/) {
  40.         $Origin = $Field[2];    # set to a different origin
  41.     
  42.         next;
  43.         }
  44.  
  45.     if (/^[*\.\-0-9A-Za-z]+/) { 
  46.         $Name = &make_name($Field[1], $Origin);
  47.     }
  48.  
  49.     if (/\tIN\tSOA\t/) {
  50.         $Zone = $Name;        # there's a new zone
  51.     }
  52.         elsif (/\tIN\tMX\t/) {
  53.         # add a new MX RR to the list
  54.         &new_MX($Zone, $Name, $Field[$#Field - 1], $Field[$#Field]);
  55.         }
  56.         elsif (/\tIN\tCNAME\t/) {
  57.             $IsAlias{$Name} = 1;    # this name is an alias
  58.         }
  59. }
  60.  
  61. foreach $Zone (keys %MX) {
  62.     &show_zone($Zone);
  63. }
  64.  
  65. exit 0;
  66.  
  67.  
  68. #
  69. # examine each of the $zone's MX's, and print the anomalies
  70. #
  71. sub show_zone {
  72.     local($zone) = @_;
  73.     local(@MXlist, $name, $rest, @NMX);
  74.  
  75.         print " ### ", &toupper($zone), " ###";
  76.  
  77.     @MXlist = split($LISTsep, $MX{$zone});    # this is the list of 
  78.                         # the zone's MX's
  79.                         
  80.         undef %NMX;    # or else it survives between calls
  81.  
  82.     while ($mx = shift(@MXlist)) {
  83.         # get the values of the RR...
  84.                 ($name, $rest) = split($ELEMsep2, $mx);
  85.  
  86.         # ... and put them in the "private" list for this name
  87.                 $NMX{$name} = &add_list($NMX{$name}, $rest);
  88.         }
  89.  
  90.         foreach $name (keys %NMX) {
  91.                 &check_MX($name, $NMX{$name});
  92.         }
  93.  
  94.     print "\n";
  95. }
  96.  
  97.  
  98. #
  99. # find out if there's anything wrong with the MX's for this $name
  100. #
  101. sub check_MX {
  102.     local($name, $MXlist) = @_;
  103.     local($mx, $str);
  104.     local($weight, $pointer, *Xmx, *Amx, *MXarray);
  105.     local($min_out, $s, $a, $e);
  106.     
  107.     $min_out = -1;
  108.     @MXarray = split($LISTsep, $MXlist);
  109.  
  110.     # scan the records and get those pointing to external hosts
  111.         foreach $mx ($[ .. $#MXarray) {
  112.         ($weight, $pointer) = split($ELEMsep1, $MXarray[$mx]);
  113.  
  114.         if (!&is_below_cut($zone, $pointer)) {
  115.                 $str = sprintf("%s%-20s IN  MX %4d  %s", 
  116.                         $Lpad[4], $name, $weight, $pointer);
  117.  
  118.             push(@Xmx, $str);
  119.  
  120.             # find the value for the most preferable external host
  121.                 if (($min_out > $weight) || ($min_out < 0)) {
  122.                 $min_out = $weight
  123.                 }
  124.         }
  125.         }
  126.  
  127.         if ($#Xmx && $Level >= 4) {
  128.         ($s, $a) = $#Xmx > 1 ? ("s", "") : ("", " an");
  129.  
  130.             print "\n$Lpad[4]MX record$s pointing to$a external host$s";
  131.  
  132.         foreach $str ($[ .. $#Xmx) {
  133.             print shift(@Xmx);
  134.         }
  135.     }
  136.  
  137.     # get the internal hosts which are less preferred than external ones
  138.         if ($min_out >= 0 && $Level >= 4) {
  139.         foreach $mx ($[ .. $#MXarray) {
  140.             ($weight, $pointer) = split($ELEMsep1, $MXarray[$mx]);
  141.  
  142.                 if (&is_below_cut($zone, $pointer) && 
  143.                     ($weight >= $min_out)) {    
  144.                 $str = sprintf("%s%-20s IN  MX %4d  %s", 
  145.                                             $Lpad[4], $name, 
  146.                         $weight, $pointer);
  147.  
  148.                 push(@Xmx, $str);
  149.             }
  150.         }
  151.  
  152.         if ($#Xmx) {
  153.             $s = $#Xmx > 1 ? "s" : "";
  154.  
  155.             print "\n$Lpad[4]MX record$s to local or delegated host$s after MX records to external hosts";
  156.  
  157.                     foreach $str ($[ .. $#Xmx) {
  158.                             print shift(@Xmx);
  159.                     }
  160.         }
  161.     }
  162.  
  163.     if ($Level >= 2) {
  164.         # get the MX RR's pointing to aliases
  165.             foreach $mx ($[ .. $#MXarray) {
  166.             ($weight, $pointer) = split($ELEMsep1, $MXarray[$mx]);
  167.  
  168.             if ($IsAlias{$pointer}) {
  169.                 $str = sprintf("%s%-20s IN  MX %4d  %s", 
  170.                     $Lpad[2], $name, $weight, $pointer);
  171.  
  172.                 push(@Amx, $str);
  173.             }
  174.             }
  175.  
  176.         if ($#Amx) {
  177.             ($s, $e, $a) = $#Amx > 1 ? ("s", "es", "") : 
  178.                            ("", "", " an");
  179.  
  180.             print "\n$Lpad[2]MX record$s pointing to$a alias$e";
  181.  
  182.             foreach $str ($[ .. $#Amx) {
  183.                 print shift(@Amx);
  184.             }
  185.         }
  186.     }
  187. }
  188.  
  189.  
  190. #
  191. # add ($name, $weight, $pointer) tupple to the list of MX record for this $zone
  192. #
  193. sub new_MX {
  194.     local($zone, $name, $weight, $pointer) = @_;
  195.     local($tuple);
  196.  
  197.     $tuple = join($ELEMsep2, $name, join($ELEMsep1, $weight, $pointer));
  198.  
  199.         $MX{$zone} = &add_list($MX{$zone}, $tuple);
  200. }
  201.  
  202.